﻿using Common.Contract.Authentication.Record;
using Common.Contract.SystemConfig.Record;
using Customer.Core.Data.Repository;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Provider
{
    public class GenericProvider
    {
        protected CustomerRepository repository = CustomerRepository.Current;

        protected IQueryable<CustomerBranch> FilterCustomerRegionByCondition(IQueryable<CustomerBranch> querySearch, UserRecord dao, FilterRecord textSearch, string lang)
        {
            DateTime ToDateCompare = DateTime.Now.AddMonths(1);
            DateTime FromDateCompare = DateTime.Now;
            if (!string.IsNullOrEmpty(textSearch.Region) && textSearch.Region.Trim() != "null")
            {
                querySearch = querySearch.Where(x => textSearch.Region.ToUpper().Contains(x.Branch.Region.ToUpper()));
            }
            if (!string.IsNullOrEmpty(textSearch.Branch) && textSearch.Branch.Trim() != "null")
            {
                querySearch = querySearch.Where(x => textSearch.Branch.ToUpper().Contains(x.Cust.BranchId.ToUpper()));
            }
            if (!string.IsNullOrEmpty(dao.Team) && dao.DaoSubOrdinatorList != null && dao.DaoSubOrdinatorList.Count > 0)
            {
                querySearch = querySearch.Where(x => dao.DaoSubOrdinatorList.Contains(x.Cust.Dao));
            }
            if (!string.IsNullOrEmpty(textSearch.Dao) && textSearch.Dao.Trim() != "null")
            {
                querySearch = querySearch.Where(x => textSearch.Dao.Contains(x.Cust.Dao));
            }
            if (!string.IsNullOrEmpty(textSearch.DaoName) && textSearch.DaoName.Trim() != "null")
            {
                querySearch = querySearch.Where(x => textSearch.DaoName.Contains(x.Cust.Dao));
            }
            if (!string.IsNullOrEmpty(textSearch.Cif))
            {
                querySearch = querySearch.Where(x => x.Cust.Cif.Contains(textSearch.Cif));
            }
            if (!string.IsNullOrEmpty(textSearch.CustName))
            {
                querySearch = querySearch.Where(x => x.Cust.CusName.Contains(textSearch.CustName));
            }
            if (!string.IsNullOrEmpty(textSearch.Industry))
            {
                querySearch = lang.Equals("vi") ? querySearch.Where(x => x.Cust.IndustryName.Contains(textSearch.Industry)) : querySearch.Where(x => x.Cust.IndustryNameEn.Contains(textSearch.Industry));
            }
            if (!string.IsNullOrEmpty(textSearch.CusOpenDate))
            {
                List<string> listFilter = textSearch.CusOpenDate.Split('-').ToList();
                FromDateCompare = DateTime.ParseExact(listFilter.FirstOrDefault().Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                ToDateCompare = DateTime.ParseExact(listFilter.LastOrDefault().Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                querySearch = querySearch.Where(x => x.Cust.CusOpenDate.Value >= FromDateCompare && x.Cust.CusOpenDate.Value <= ToDateCompare);
            }
            if (!string.IsNullOrEmpty(textSearch.CusStatus) && textSearch.CusStatus != "2")
            {
                querySearch = querySearch.Where(x => x.Cust.CusStatus.Equals(textSearch.CusStatus));
            }
            if (!string.IsNullOrEmpty(textSearch.BusinessStatus) && textSearch.BusinessStatus != "2")
            {
                querySearch = querySearch.Where(x => x.Cust.BusinessStatus.Equals(textSearch.BusinessStatus));
            }
            return querySearch;
        }
        protected class CustomerBranch
        {
            public CustomerRegion Cust { get; set; }
            public Branch Branch { get; set; }
        }
    }
}
